package nanzhuli.user.svc.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {

    private static Logger logger = LoggerFactory.getLogger(MVCConfig.class);

//    @Autowired
//    private CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver;
//    /**
//     * 配置ArgumentResolvers
//     * 实现采用业务逻辑，向controllor 方法中注入参数
//     * @param argumentResolvers
//     */
//    @Override
//    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//        argumentResolvers.add(currentUserMethodArgumentResolver);
//    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //可以通过http://127.0.0.1:8080/web/home.html访问resources/web/home.html页面
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//        super.addResourceHandlers(registry);

        if (logger.isDebugEnabled()) {
            logger.debug("addResourceHandlers, [/static/**, swagger-ui.html, /webjars/**]");
        }
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 跨域CORS配置
     * @param registry 请求
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        super.addCorsMappings(registry);
        registry.addMapping("/cors/**")
                .allowedHeaders("*")
                .allowedMethods("POST","GET", "PUT", "DELETE")
                .allowedOrigins("*");
    }

//    /**
//     * 拦截器配置
//     * @param registry
//     */
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new CustomHandlerInterceptor()).addPathPatterns("/**");
//    }

}
